<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				
				<el-button type="primary" plain icon="el-icon-briefcase" :disabled="selection.length==0" @click="stockFinance">结算打勾项</el-button>
			</div>

			<div class="right-panel">
				<div class="right-panel-search">
					<el-select placeholder="请选择类型" v-model="searchOption.stock_type">
						<el-option label="入库" value="1"></el-option>
						<el-option label="发货" value="2"></el-option>
					</el-select>

					<el-select placeholder="是否结算" v-model="searchOption.is_balance">
						<el-option label="已结算" value="1"></el-option>
						<el-option label="未结算" value="0"></el-option>
					</el-select>


					<el-select placeholder="选择客户" v-model="searchOption.company_id">
						<el-option label="全部" value=""></el-option>
						<el-option v-for="item in companyOptions" :key="item.id" :label="item.company_name" :value="item.id"></el-option>
					</el-select>

					<el-input placeholder="扫描记录数量" v-model="searchOption.pageCount" style="width: 120px"/>
					<el-date-picker value-format="YYYY-MM-DD" v-model="searchOption.created_at" type="date" placeholder="选择时间"></el-date-picker>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="list.apiObj" row-key="id" @dataChange="tableLoad" @selection-change="selectionChange" stripe>
				<el-table-column :selectable="isBalance" type="selection" width="50"></el-table-column>
				<el-table-column label="是否结算" prop="stock_type" width="95">
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.is_balance == 1"  effect="dark" style="font-weight: bold; font-size: 16px">已结算 </el-tag>
						<el-tag type="warning" v-if="scope.row.is_balance == 0"  effect="dark" style="font-weight: bold; font-size: 16px">未结算</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="类型" prop="stock_type" width="85">
					<template #default="scope">
						<el-tag  style="font-weight: bold; font-size: 18px" v-if="scope.row.stock_type == 1">入库</el-tag>
						<el-tag  style="font-weight: bold; font-size: 18px" type="success" v-if="scope.row.stock_type == 2">发货</el-tag>
					</template>
				</el-table-column>
				
				<el-table-column label="货号" prop="goods_number" width="150">
					<template #default="scope">
						<el-tag   effect="dark" style="font-weight: bold; font-size: 20px">{{scope.row.goods_number}}</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="颜色" prop="color" width="95">
					<template #default="scope">
						<el-tag type="info"  effect="dark" style="font-weight: bold; font-size: 16px;">{{scope.row.color}}</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="尺码" prop="size" width="90">
					<template #default="scope">
						<el-tag type="info"  effect="dark" style="font-weight: bold; font-size: 16px;">{{scope.row.size}}</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="描述" prop="goods_desc" width="180"></el-table-column>
				<el-table-column label="客户" prop="company_name" width="60"></el-table-column>
				<el-table-column label="单价" prop="price" width="60"></el-table-column>
				<el-table-column label="库存量" prop="stock" width="60"></el-table-column>
				<el-table-column label="小计" prop="price_total" width="100"></el-table-column>
				
				<el-table-column label="时间" prop="created_at" width="150"></el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="100">
					<template #default="scope">
						<el-button v-if="scope.row.is_balance == 1" type="primary" plain size="small" @click="finance(scope.row)">查看</el-button>
						<el-button v-if="scope.row.is_balance == 0" type="primary" plain size="small" @click="table_edit(scope.row)">编辑</el-button>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>

	<el-drawer v-model="dialog.info" :size="800" title="详细" direction="rtl" destroy-on-close>
		<info ref="infoDialog"></info>
	</el-drawer>

</template>

<script>
	import saveDialog from './save'
	import info from './info'

	export default {
		name: 'listCrud',
		components: {
			saveDialog,
			info
		},
		data() {
			return {
				dialog:{
					save: false,
					info: false
				},
				list: {
					apiObj: this.$API.stock.list
				},
				selection: [],
				searchOption: {
					stock_type: "",
					is_balance: "",
					pageCount: "",
					created_at: "",
				},
				companyOptions: [],
				signOptions: [],
			}
		},
		mounted() {
			this.$refs.table.$nextTick(() => {
				console.log(this.$refs.table.tableData)
			})
			
		},
		methods: {
			upsearch(page = 1) {
				this.$refs.table.upData(this.searchOption, page)		
			},
			//查看 财务
			finance(row) {
				this.$router.push({
					path: '/template/list/crud/detail',
					query: {
						stock_order_id: row.id
					}
				})
			},
			//禁用已经结算的
			isBalance(row) {

				if (row.is_balance) {
					return false
				} else {
					return true
				}
			},
			//窗口新增
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//窗口编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			
			//查看
			table_show(row){
				this.dialog.info = true
				this.$nextTick(() => {
					this.$refs.infoDialog.setData(row)
				})
			},
			//删除明细
			async table_del(row, index){
				var reqData = {id: row.id}
				var res = await this.$API.demo.post.post(reqData);
				if(res.code == 200){
					this.$refs.table.removeIndex(index)
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//批量结算
			async stockFinance(){
				var confirmRes = await this.$confirm(`确定结算选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning',
					confirmButtonText: '结算',
					confirmButtonClass: 'el-button--danger'
				}).catch(() => {})

				if(!confirmRes){
					return false
				}

				var ids = this.selection.map(v => v.id)
				var reqData = {ids: ids}
				var res = await this.$API.stock.finance.post(reqData);

				if (res.code == 200) {
					var page = this.$refs.table.currentPage

					this.upsearch(page)
					this.$message.success("操作成功")
				} else {
					this.$message.error(res.message)
				}
				
				

			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection
			},
			//本地更新数据
			handleSaveSuccess(data, mode){
				//为了减少网络请求，直接变更表格内存数据
				if(mode=='add'){
					this.$refs.table.unshiftRow(data)
				}else if(mode=='edit'){
					this.$refs.table.updateKey(data)
				}

				//当然也可以暴力的直接刷新表格
				// this.$refs.table.refresh()
			},
			// 表格加载完成
			tableLoad(res) {
				console.log(res)
				this.signOptions = res.sign
				this.companyOptions = res.company
			}
		}
	}
</script>

<style>
</style>
